// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// y [author] rhyme_lph
// e [email]  rhymelph@gmail.com
// l [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:dio/src/response.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/expense_detail_entity.dart';
import 'package:finance_app/src/entities/transaction_entity.dart';
import 'package:finance_app/src/http_service/http_transaction_service.dart';
import 'package:finance_app/src/mixin/check_plan_mixin.dart';
import 'package:finance_app/src/mixin/refresh_mixin.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:flutter/material.dart';

class LinkedExpenseProvider extends BaseProvider<List<TransactionEntity>>
    with
        SearchDataListMixin,
        RefreshMixin<List<TransactionEntity>, List<TransactionEntity>>,
        CheckPlanMixin {
  TransactionEntity select;

  @override
  Future get dataFuture =>
      HttpTransactionService.getTransaction(page: page, type: [
        'expense',
      ]);

  void onNavAddExpense() async {
    if (isPlanExpired()) return;
    final ExpenseDetailEntity result =
        await Routes.navigateTo(RouterGenProviders.newExpensePage, params: {
      'isSelectMode': true,
    });

    if (result != null) {
      select = TransactionEntity()
        ..name = result.contact.name
        ..total = result.total
        ..transactionType = 'expense'
        ..transactionableId = result.id;
      print("select $select");
      onDone();
    }
  }

  void onSelect(TransactionEntity item1) {
    select = item1;
    onDone();
  }

  void onDone() {
    obtainContext?.call((context) {
      Navigator.of(context).pop(select);
    });
  }

  void onCancel() {
    obtainContext?.call((context) {
      Navigator.of(context).pop();
    });
  }

  @override
  void searchRefreshData(data) {}

  @override
  FutureOr<List<TransactionEntity>> transformData(
      List<TransactionEntity> d, bool isCache) {
    return super.transformData(d, isCache);
  }

  @override
  List<TransactionEntity> get dataList => data;

  @override
  List<TransactionEntity> getFromRequestResult(
      Response result, bool isRefresh) {
    return getEntityFromResult<List<TransactionEntity>>(result);
  }

  @override
  Future get refreshFuture => dataFuture;
}
